<?php
// include the breadcrumbs
require_once(INCLUDEPATH . 'scripts/breadcrumbs/breadcrumbs.php');

/**
 * Set up the theme and include other functions files
 */
function includeFunctions()
{
	if (file_exists(INCLUDEPATH . 'functions/meta.php')) {
		include(INCLUDEPATH . 'functions/meta.php');
	}

	if (file_exists(INCLUDEPATH . 'functions/content.php')) {
		include(INCLUDEPATH . 'functions/content.php');
	}
	
	if (file_exists(INCLUDEPATH . 'functions/shortcodes.php')) {
		include(INCLUDEPATH . 'functions/shortcodes.php');
	}

	if (file_exists(INCLUDEPATH . 'functions/widgets.php')) {
		include(INCLUDEPATH . 'functions/widgets.php');
	}
	
	if (file_exists(INCLUDEPATH . 'admin/echotheme-options.php')) {
		include(INCLUDEPATH . 'admin/echotheme-options.php');
	}
	
	if ( !function_exists( 'optionsframework_init' ) ) {
		define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/inc/options/' );
		require_once dirname( __FILE__ ) . '/../options/options-framework.php';
	}
}

includeFunctions();

/**
 * Set up featured image support on pages
 */
function echotheme_setup()
{
	// This theme uses post thumbnails
	add_theme_support( 'post-thumbnails' );
	
	add_image_size( 'fours', 200, 150, true );
	add_image_size( 'one-third', 300, 225, true );
	add_image_size( 'two-thirds', 620, 465, true );
	add_image_size( 'slideshow' , 1600, 400, true);
	add_image_size( 'flex-gallery', 720, 405, true);
	
	// remove_filter ('the_content', 'wpautop');
	
	register_nav_menus(array( 
		'header-menu' 	=> 'Header Area Menu',
		'top-menu' 		=> 'Horizontal Nav Bar',
		'sidebar-menu' 	=> 'Sidebar Area Menu',
		'footer-menu' 	=> 'Footer Area Menu'
	));
	
	// Skeleton and Base Style
	wp_register_style('base', get_bloginfo('template_url') . '/css/base.css', false, '', 'screen');
	wp_register_style('skeleton', get_bloginfo('template_url') . '/css/skeleton-960-40-10.css', array('base'), '', 'screen');
    wp_register_style('layout', get_bloginfo('template_url') . '/css/layout.css', array('skeleton'), '', 'screen');
	wp_register_style('theme', get_bloginfo('template_url') . '/css/theme.css', array('layout'), '', 'screen');
	wp_register_style('style', get_bloginfo('template_url') . '/style.css', null, '', 'screen');
	
	// for admin panel generated Style
	wp_register_style('custom-style', get_bloginfo('template_url') . '/inc/admin/custom_style.php', array('theme'), '', 'screen');
	
	// Superfish Functionality
	wp_register_style('superfish', get_bloginfo('template_url') . '/inc/scripts/superfish/css/superfish.css');
	wp_register_script('hoverintent', get_bloginfo('template_url') . '/inc/scripts/superfish/js/hoverintent.js', array('jquery'));
	wp_register_script('superfish', get_bloginfo('template_url') . '/inc/scripts/superfish/js/superfish.js', array('hoverintent'));
}

add_action('after_setup_theme', 'echotheme_setup');

/**
 * Action hook to create custom post types
 *
 */
function create_post_type()
{
	register_post_type('echotheme_slideshow',
		array(
			'labels' => array(
				'name' => __('Slide Show'),
				'singular_name' => __('Slide'),
				'all_items' => __('All Slides'),
				'add_new' => __('Add New Slide'),
				'add_new_item' => __('Add A New Slide'),
				'new_item' => __('Add Slide'),
				'edit_item' => __('Edit Slide'),
			),
			'description' => 'Use this panel to add images to the site slide show',
			'public' => true,
			'exclude_from_search' => true,
			'menu_position' => 25,
			'supports' => array(
				'title',
				'editor',
				'custom-fields',
				'excerpt',
				'thumbnail',
				'page-attributes'
			),
			// 'hierarchical' => true,
			'show_in_nav_menus' => false,
		)
	);
}
add_action('init', 'create_post_type');

/**
 * Shortcut to get post custom field
 * @param string $name requested custom field
 * @return string $value
 */
function get_custom_value($name = null)
{
	if ($name == null) {
		return null;
	}
	
	$val = get_post_custom_values($name);
	return $val[0];
}

/**
 * function getTemplate
 *
 * @param string $section 
 * @param string $template 
 * @return void
 */
function getTemplate($section = null, $template = null)
{
	global $post;
	if ($section === null || $template === null) {
		return null;
	}
	
	$sep = '/';
	$template = THEMETEMPLATES . $sep . $section . $sep . $template;

	if (file_exists($template . '.phtml')) {
		include($template . '.phtml');
	} else if (file_exists($template . '.php')) {
		include($template . '.php');
	}
}

/**
 * Tests if any of a post's assigned categories are descendants of target categories
 *
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7+
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
function test_post_is_in_descendant_category( $cats, $_post = null )
{
    foreach ( (array) $cats as $cat ) {
	// get_term_children() accepts integer ID only
	$descendants = get_term_children( (int) $cat, 'category');
	if ( $descendants && in_category( $descendants, $_post ) )
	return true;
    }
    return false;
}

/**
 * Tests if any of a post's assigned categories are in the target categories or in any of the descendants
 *
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7+
 */
function post_is_in_category_or_descendants( $cats, $_post = null )
{
    if( in_category( $cats, $_post = null ) || test_post_is_in_descendant_category( $cats, $_post = null ) ) {
	return true;
    }
    return false;
}

/**
 * This function is used to generate custom breadcrumbs for single posts view. Portfolio section or regular Blog is considered
 * when generating the link structure.
 */
function get_category_parents_for_breadcrumbs( $id, $link = false, $separator = '/' ) {
	global $udesign_options, $portfolio_pages_array;
	$portfolio_cats_array = explode( ',', $udesign_options['portfolio_categories'] );
	if ( post_is_in_category_or_descendants($portfolio_cats_array) ) { // if the current post belongs to any Porfolio category
	    foreach ( $portfolio_pages_array as $portfolio_page_obj ) {
		$port_page_ID = $portfolio_page_obj->ID;
		if ( post_is_in_category_or_descendants( $udesign_options['portfolio_cat_for_page_'.$port_page_ID] ) ) {
		    echo get_category_parents_for_portfolio_page( $id, $link, $separator, FALSE , $port_page_ID );
		    break;
		}
	    }
	} else { // if the current category is a regular blog category
	    echo get_category_parents( $id, $link, $separator, FALSE );
	}
}
?>